//
//  ShadowView.swift
//  FastDevelop
//
//  Created by IOS1 on 2025/6/27.
//

import UIKit

class ShadowView: UIView {
    // MARK: - 可配置属性
    @IBInspectable var cornerRadius: CGFloat = 0 {
        didSet {
            updateAppearance()
        }
    }
    
    @IBInspectable var shadowColor: UIColor = .black {
        didSet {
            updateAppearance()
        }
    }
    
    @IBInspectable var shadowOpacity: Float = 0.5 {
        didSet {
            updateAppearance()
        }
    }
    
    @IBInspectable var shadowOffset: CGSize = .zero {
        didSet {
            updateAppearance()
        }
    }
    
    @IBInspectable var shadowRadius: CGFloat = 3 {
        didSet {
            updateAppearance()
        }
    }
    
    @IBInspectable var borderWidth: CGFloat = 0 {
        didSet {
            updateAppearance()
        }
    }
    
    @IBInspectable var borderColor: UIColor = .clear {
        didSet {
            updateAppearance()
        }
    }
    
    // MARK: - 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }
    
    private func commonInit() {
        backgroundColor = .white
        updateAppearance()
    }
    
    // MARK: - 布局
    override func layoutSubviews() {
        super.layoutSubviews()
        updateAppearance()
    }
    
    // MARK: - 更新外观
    private func updateAppearance() {
        // 圆角设置
        layer.cornerRadius = cornerRadius
        layer.borderWidth = borderWidth
        layer.borderColor = borderColor.cgColor
        
        // 阴影设置
        layer.shadowColor = shadowColor.cgColor
        layer.shadowOpacity = shadowOpacity
        layer.shadowOffset = shadowOffset
        layer.shadowRadius = shadowRadius
        
        // 关键：确保阴影不被裁剪
        layer.masksToBounds = false
        
        // 优化性能：设置阴影路径
        let shadowPath = UIBezierPath(
            roundedRect: bounds,
            cornerRadius: cornerRadius
        )
        layer.shadowPath = shadowPath.cgPath
    }
}
